/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
    SPDX-FileCopyrightText: 2017 Roman Gilg <subdiff@gmail.com>
    SPDX-FileCopyrightText: 2018 Furkan Tokac <furkantokac34@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

import QtQuick 2.7
import QtQuick.Controls 2.0 as QQC2
import QtQuick.Layouts 1.3 as Layouts

import org.kde.kcm 1.1 as KCM
import org.kde.kirigami 2.13 as Kirigami

import org.kde.touchpad.kcm 1.0
import org.kde.plasma.core 2.0 as PlasmaCore
// TODO: Change ScrollablePage as KCM.SimpleKCM
// after rewrite the KCM in KConfigModule.
Kirigami.ScrollablePage {
    id: root

    spacing: Kirigami.Units.smallSpacing
    signal changeSignal()
    property bool lighttheme:PlasmaCore.Theme.viewBackgroundColor=="#ffffff" 
    property var backgroundcolor: PlasmaCore.Theme.viewBackgroundColor
    property var textcolor:PlasmaCore.Theme.textColor
    property var unabletextcolor:textcolor
    property var spaceline:lighttheme?"#F0F0F0":"#2D2D2D"
    property var bordercolor:lighttheme?"#CCCCCC":"#2D2D2D"

    property alias deviceIndex: deviceSelector.currentIndex
    property QtObject touchpad
    property int touchpadCount: backend.touchpadCount

    property bool loading: false

    function resetModel(index) {
        touchpadCount = backend.touchpadCount
        formLayout.enabled = touchpadCount
        deviceSelector.enabled = touchpadCount > 1

        loading = true
        if (touchpadCount) {
            touchpad = deviceModel[index]
            deviceSelector.model = deviceModel
            deviceSelector.currentIndex = index
        } else {
            deviceSelector.model = [""]
        }
        loading = false
    }

    function syncValuesFromBackend() {
        loading = true
        deviceEnabled.load()
        dwt.load()
        leftHanded.load()
        middleEmulation.load()
        accelSpeedSpinbox.load()
        accelSpeedSlider.load()
        accelProfile.load()
        tapToClick.load()
        tapAndDrag.load()
        tapAndDragLock.load()
        multiTap.load()
        scrollMethod.load()
        naturalScroll.load()
        scrollFactor.load()
        rightClickMethod.load()
        middleClickMethod.load()
        disableHorizontalScrolling.load()
        loading = false
    }

    function updateview(){
        formLayout.height = deviceitem.height+normalitem.height+pointeritem.height+tappingitem.height+twofingeritem.height+scrollitem.height+rightclickitem.height+midclickitem.height
    }

    header: Kirigami.InlineMessage {
        id: inlineMessage
    }

    Connections {
        target: TouchpadConfig

        function onShowMessage(message, type) {

            if (touchpadCount === 0) {
                return
            }

            if (message.length !== 0) {
                inlineMessage.text = message
                inlineMessage.type = type
                inlineMessage.visible = true
            } else {
                inlineMessage.visible = false
            }
        }
    }

    Kirigami.PlaceholderMessage {
        text: i18nd("kcm_touchpad", "No touchpad found")
        icon.name: "edit-none"
        anchors.centerIn: parent
        visible: touchpadCount === 0
        width: parent.width - (Kirigami.Units.largeSpacing * 4)
    }

    Rectangle {
        id:formLayout
        Layouts.Layout.fillWidth: true
        color:"transparent"
        visible:touchpadCount > 0
        height:deviceitem.height+normalitem.height+pointeritem.height+tappingitem.height+twofingeritem.height+scrollitem.height+rightclickitem.height+midclickitem.height
        //Device
        Rectangle{
            id:deviceitem
            visible: touchpadCount > 1
            anchors.top : parent.top
            anchors.horizontalCenter:parent.horizontalCenter
            color: backgroundcolor
            border.color: bordercolor
            border.width: 1
            radius: 10
            height:touchpadCount > 1 ?48:0
            width:558
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Device")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }


            QQC2.ComboBox {
                id: deviceSelector
                model: deviceModel
                anchors.verticalCenter:parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 344
                width: 192
                height:30
                textRole: "name"

                onCurrentIndexChanged: {
                    if (touchpadCount) {
                        touchpad = deviceModel[currentIndex]
                        if (!loading) {
                            changeSignal()
                        }
                    }
                    root.syncValuesFromBackend()
                }
            }
        }

        //NORMAL
        Rectangle{
            id:normalitem
            anchors.top : deviceitem.bottom
            anchors.horizontalCenter:parent.horizontalCenter
            color: "transparent" 
            height:222
            width:558
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 10
                anchors.left: parent.left
                text: i18nd("kcm_touchpad", "General")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            Rectangle {
                anchors.top: parent.top
                anchors.topMargin: 33
                anchors.left: parent.left
                color: backgroundcolor 
                height:189
                width:558
                border.color: bordercolor
                border.width: 1
                radius: 10

                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 15
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Device enabled")
                    font.pixelSize: 14
                    color:  deviceEnabled.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id: deviceEnabled
                    anchors.top: parent.top
                    anchors.topMargin: 15
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    hoverEnabled: true
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Accept input through this device.")
                        visible: parent.hovered
                        delay: 1000
                    }
                    function load() {
                        if (!formLayout.enabled) {
                            checked = false
                            return
                        }
                        enabled = touchpad.supportsDisableEvents
                        checked = enabled && touchpad.enabled
                    }
                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.enabled = checked
                            root.changeSignal()
                        }
                    }
                }
                Rectangle{
                    anchors.top: parent.top
                    anchors.topMargin: 47
                    width:parent.width 
                    color:spaceline
                    height:1
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 61
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Disable while typing")
                    font.pixelSize: 14
                    color: dwt.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id: dwt
                    anchors.top: parent.top
                    anchors.topMargin: 61
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Disable touchpad while typing to prevent accidental inputs.")
                        visible: parent.hovered
                        delay: 1000
                    }
                    function load() {
                        if (!formLayout.enabled) {
                            checked = false
                            return
                        }
                        enabled = touchpad.supportsDisableWhileTyping
                        checked = enabled && touchpad.disableWhileTyping
                    }

                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.disableWhileTyping = checked
                            root.changeSignal()
                        }
                    }

                }
                Rectangle{
                    anchors.top: parent.top
                    anchors.topMargin: 94
                    width:parent.width 
                    color:spaceline
                    height:1
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 109
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Left handed mode")
                    font.pixelSize: 14
                    color:  leftHanded.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id: leftHanded
                    anchors.top: parent.top
                    anchors.topMargin: 109
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Swap left and right buttons.")
                        visible: parent.hovered
                        delay: 1000
                    }

                    function load() {
                        if (!formLayout.enabled) {
                            checked = false
                            return
                        }
                        enabled = touchpad.supportsLeftHanded
                        checked = enabled && touchpad.leftHanded
                    }

                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.leftHanded = checked
                            root.changeSignal()
                        }
                    }

                }
                Rectangle{
                    anchors.top: parent.top
                    anchors.topMargin: 141
                    width:parent.width 
                    color:spaceline
                    height:1
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 155
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Press left and right buttons for middle click")
                    font.pixelSize: 14
                    color:  middleEmulation.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id:middleEmulation
                    anchors.top: parent.top
                    anchors.topMargin: 155
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Clicking left and right button simultaneously sends middle button click.")
                        visible: parent.hovered
                        delay: 1000
                    }

                    function load() {
                        if (!formLayout.enabled) {
                            checked = false
                            return
                        }
                        enabled = touchpad.supportsMiddleEmulation
                        checked = enabled && touchpad.middleEmulation
                    }

                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.middleEmulation = checked
                            root.changeSignal()
                        }
                        loading = true
                        middleClickMethod.load()
                        root.updateview()
                        loading = false
                    }

                }

            }
        }

        //Pointer
        Rectangle{
            id:pointeritem
            anchors.top : normalitem.bottom
            anchors.horizontalCenter:parent.horizontalCenter
            color: "transparent" 
            height:134
            width:558
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 16
                anchors.left: parent.left
                text: i18nd("kcm_touchpad", "Pointer")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            Rectangle {
                id:accelSpeed
                anchors.top: parent.top
                anchors.topMargin: 39
                anchors.left: parent.left
                color: backgroundcolor
                height:95
                width:558
                border.color: bordercolor
                border.width: 1
                radius: 10
                function onAccelSpeedChanged(val) {
                    // check slider
                    if (val != accelSpeedSlider.accelSpeedValue) {
                        accelSpeedSlider.accelSpeedValue = val
                        accelSpeedSlider.value = Math.round(6 + (val / 100) / 0.2)
                    }

                    // check spinbox
                    if (val != accelSpeedSpinbox.value) {
                        accelSpeedSpinbox.value = val
                    }

                    // check libinput accelspeed
                    if ((val / 100) != touchpad.pointerAcceleration) {
                        touchpad.pointerAcceleration = val / 100
                        root.changeSignal()
                    }
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 15
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Pointer acceleration")
                    font.pixelSize: 14
                    color: accelSpeedSlider.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }

                KcmSlider {
                    anchors.top: parent.top
                    anchors.topMargin: 24
                    anchors.left: parent.left
                    anchors.leftMargin:210
                    id: accelSpeedSlider
                    from: 1
                    to: 11
                    stepSize: 1
                    property int accelSpeedValue: 0 // [-100, 100]
                    function load() {
                        enabled = touchpad.supportsPointerAcceleration
                        if (!enabled) {
                            return
                        }
                        accelSpeedValue = Math.round(touchpad.pointerAcceleration * 100)
                        // convert libinput pointer acceleration range [-1, 1] to slider range [1, 11]
                        value = Math.round(6 + touchpad.pointerAcceleration / 0.2)
                    }
                    onValueChanged: {
                        if (touchpad != undefined && enabled && !root.loading) {
                            // convert slider range [1, 11] to accelSpeedValue range [-100, 100]
                            accelSpeedValue = Math.round(((value - 6) * 0.2) * 100)
                            accelSpeed.onAccelSpeedChanged(accelSpeedValue)
                        }
                    }
                }
                QQC2.SpinBox {
                    id: accelSpeedSpinbox
                    Layouts.Layout.minimumWidth: Kirigami.Units.gridUnit * 4
                    anchors.top: parent.top
                    anchors.topMargin: 9
                    anchors.left: parent.left
                    anchors.leftMargin:440
                    width:96
                    height:30
                    from: -100
                    to: 100
                    stepSize: 1
                    editable: true
                    validator: DoubleValidator {
                        bottom: accelSpeedSpinbox.from
                        top: accelSpeedSpinbox.to
                    }
                    function load() {
                        enabled = touchpad.supportsPointerAcceleration
                        if (!enabled) {
                            return
                        }
                        value = Math.round(touchpad.pointerAcceleration * 100)
                    }
                    onValueChanged: {
                        if (touchpad != undefined && enabled && !root.loading) {
                            accelSpeed.onAccelSpeedChanged(value)
                        }
                    }
                    textFromValue: function(val, locale) {
                        return Number(val / 100).toLocaleString(locale, "f", 2)
                    }
                    valueFromText: function(text, locale) {
                        return Number.fromLocaleString(locale, text) * 100
                    }
                }
                
                Rectangle{
                    anchors.top: parent.top
                    anchors.topMargin: 48
                    width:parent.width 
                    color:spaceline
                    height:1
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 61
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Acceleration profile")
                    font.pixelSize: 14
                    color: accelProfile.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                QQC2.ComboBox {
                        id: accelProfile
                        anchors.top: parent.top
                        anchors.topMargin: 56
                        anchors.left: parent.left
                        anchors.leftMargin: 344
                        width: 192
                        height:30
                        model: [i18nd("kcm_touchpad", "Flat"),i18nd("kcm_touchpad", "Adaptive")]
                        function load() {
                            enabled = touchpad.supportsPointerAccelerationProfileAdaptive
                            accelProfile.visible = enabled
                            if (!enabled) {
                                accelProfile. currentIndex=-1
                                return
                            }
                            if(touchpad.pointerAccelerationProfileAdaptive) {
                                    accelProfile. currentIndex= 1
                            } else {
                                    accelProfile. currentIndex= 0
                            }
                        }

                        function syncCurrent() {
                            if(accelProfile. currentIndex < 0){
                                return
                            }
                            if (enabled && !root.loading) {
                                // touchpad.pointerAccelerationProfileFlat = accelProfileFlat.checked
                                // touchpad.pointerAccelerationProfileAdaptive = accelProfileAdaptive.checked
                                touchpad.pointerAccelerationProfileFlat = (accelProfile. currentIndex ==0)?true : false
                                touchpad.pointerAccelerationProfileAdaptive  = (accelProfile. currentIndex ==1)?true : false
                                root.changeSignal()
                            }
                        }
                        onCurrentIndexChanged:accelProfile.syncCurrent()
                }
            }
        }

        // Tapping
        Rectangle{
            id:tappingitem
            anchors.top : pointeritem.bottom
            anchors.horizontalCenter:parent.horizontalCenter
            color: "transparent" 
            height:181
            width:558
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 16
                anchors.left: parent.left
                text: i18nd("kcm_touchpad", "Tapping")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            Rectangle {
                anchors.top: parent.top
                anchors.topMargin: 39
                anchors.left: parent.left
                color: backgroundcolor
                height:142
                width:558
                border.color:bordercolor
                border.width: 1
                radius: 10
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 15
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Tap-to-click")
                    font.pixelSize: 14
                    color: tapToClick.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id: tapToClick
                    anchors.top: parent.top
                    anchors.topMargin: 15
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Single tap is left button click.")
                        visible: parent.hovered
                        delay: 1000
                    }

                    function load() {
                        tapToClick.visible = true
                        enabled = touchpad.tapFingerCount > 0
                        checked = enabled && touchpad.tapToClick
                    }

                    function updateDependents() {
                        loading = true
                        tapAndDrag.load()
                        tapAndDragLock.load()
                        multiTap.load()
                        loading = false
                    }

                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.tapToClick = checked
                            updateDependents()
                            root.changeSignal()
                        }
                    }

                }
                Rectangle{
                    
                    anchors.top: parent.top
                    anchors.topMargin: 48
                    width:parent.width 
                    color:spaceline
                    height:1
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 61
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Tap-and-drag")
                    font.pixelSize: 14
                    color: tapAndDrag.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id: tapAndDrag
                    anchors.top: parent.top
                    anchors.topMargin: 61
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Sliding over touchpad directly after tap drags.")
                        visible: parent.hovered
                        delay: 1000
                    }

                    function load() {
                        tapAndDrag.visible = true
                        enabled = touchpad.tapFingerCount > 0 && tapToClick.checked
                        checked = enabled && touchpad.tapAndDrag
                    }

                    function updateDependents() {
                        loading = true
                        tapAndDragLock.load()
                        loading = false
                    }

                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.tapAndDrag = checked
                            updateDependents()
                            root.changeSignal()
                        }
                    }
                    
                }
                Rectangle{
                    anchors.top: parent.top
                    anchors.topMargin: 94
                    width:parent.width 
                    color:spaceline
                    height:1
                }
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 109
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Tap-and-drag lock")
                    font.pixelSize: 14
                    color: tapAndDragLock.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                KcmSwitch{
                    id: tapAndDragLock
                    anchors.top: parent.top
                    anchors.topMargin: 109
                    anchors.left: parent.left
                    anchors.leftMargin: 498
                    QQC2.ToolTip {
                        text: i18nd("kcm_touchpad", "Dragging continues after a short finger lift.")
                        visible: parent.hovered
                        delay: 1000
                    }

                    function load() {
                        enabled = touchpad.tapFingerCount > 0 && tapAndDrag.checked
                        checked = enabled && touchpad.tapDragLock
                    }

                    onCheckedChanged: {
                        if (enabled && !root.loading) {
                            touchpad.tapDragLock = checked
                            root.changeSignal()
                        }
                    }
                }

            }
        }
        //Two-finger
        Rectangle{
            id:twofingeritem
            anchors.top : tappingitem.bottom
            anchors.horizontalCenter:parent.horizontalCenter
            color: "transparent" 
            height:88
            width:558
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 16
                anchors.left: parent.left
                text: i18nd("kcm_touchpad", "Two-finger tap")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            Rectangle {
                anchors.top: parent.top
                anchors.topMargin: 39
                anchors.left: parent.left
                color: backgroundcolor
                height:48
                width:558
                border.color: bordercolor
                border.width: 1
                radius: 10
                QQC2.Label{
                    anchors.top: parent.top
                    anchors.topMargin: 15
                    anchors.left: parent.left
                    anchors.leftMargin: 23
                    text: i18nd("kcm_touchpad", "Two-finger tap")
                    font.pixelSize: 14
                    color: multiTap.enabled?textcolor:unabletextcolor
                    font.family: "SourceHanSansSC-Regular"
                }
                QQC2.ComboBox {
                    id: multiTap
                    anchors.top: parent.top
                    anchors.topMargin: 9
                    anchors.left: parent.left
                    anchors.leftMargin: 344
                    width: 192
                    height:30
                    model : [i18nd("kcm_touchpad", "Right-click (three-finger tap to middle-click)"),i18nd("kcm_touchpad", "Middle-click (three-finger tap right-click)")]
                    function load() {
                        enabled = touchpad.supportsLmrTapButtonMap && tapToClick.checked
                        // if (touchpad.tapFingerCount > 2) {
                        //     model = [ i18nd("kcm_touchpad", "Right-click (three-finger tap to middle-click)"),i18nd("kcm_touchpad", "Middle-click (three-finger tap right-click)")]
                        // } else {
                        //     model = [ i18nd("kcm_touchpad", "Right-click"),i18nd("kcm_touchpad", "Middle-click")]
                        // }
    
                        if (!enabled) {
                            return
                        }
                        if(touchpad.lmrTapButtonMap) {
                            multiTap.currentIndex=1
                        } else if(touchpad.lrmTapButtonMap){
                            multiTap.currentIndex=0
                        }
                    }

                    function syncCurrent() {
                        if (enabled && !root.loading) {
                            touchpad.lmrTapButtonMap = (multiTap.currentIndex==1)?true:false
                            touchpad.lrmTapButtonMap = (multiTap.currentIndex==0)?true:false
                            root.changeSignal()
                        }
                    }
                    onCurrentIndexChanged:multiTap.syncCurrent()
                }
            }
        }
    //SCROLL
    Rectangle{
        id:scrollitem
        anchors.top : twofingeritem.bottom
        anchors.horizontalCenter:parent.horizontalCenter
        color: "transparent" 
        // height:181
        height:scrollFactor.visible?222:181
        width:558
        QQC2.Label{
            anchors.top: parent.top
            anchors.topMargin: 16
            anchors.left: parent.left
            text: i18nd("kcm_touchpad", "Scrolling")
            font.pixelSize: 14
            color:textcolor
            font.family: "SourceHanSansSC-Regular"
        }
        Rectangle {
            anchors.top: parent.top
            anchors.topMargin: 39
            anchors.left: parent.left
            color: backgroundcolor
            // height:142
            height:scrollFactor.visible?189:142
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Scrolling")
                font.pixelSize: 14
                color: scrollMethod.enabled?textcolor:unabletextcolor
                font.family: "SourceHanSansSC-Regular"
            }
            QQC2.ComboBox {
                id:scrollMethod
                anchors.top: parent.top
                anchors.topMargin: 9
                anchors.left: parent.left
                anchors.leftMargin: 344
                width: 192
                height:30
                model : []
                function load() {
                    enabled = touchpad.supportsScrollTwoFinger || touchpad.supportsScrollEdge
                    if(!enabled){
                        scrollMethod.currentIndex=-1
                        return
                    }
                    if( touchpad.supportsScrollTwoFinger && touchpad.supportsScrollEdge){
                        model = [i18nd("kcm_touchpad", "None"),i18nd("kcm_touchpad", "Two fingers"),i18nd("kcm_touchpad", "Touchpad edges")]
                        if(touchpad.scrollEdge){
                            scrollMethod.currentIndex=2
                        }else if(touchpad.scrollTwoFinger){
                            scrollMethod.currentIndex=1
                        }else{
                            scrollMethod.currentIndex=0
                        }
                        return
                    }
                    if(touchpad.supportsScrollTwoFinger){
                        model = [i18nd("kcm_touchpad", "None"),i18nd("kcm_touchpad", "Two fingers")]
                        if(touchpad.scrollTwoFinger){
                            scrollMethod.currentIndex=1
                        }else{
                            scrollMethod.currentIndex=0
                        }
                        
                    }else{
                        model = [i18nd("kcm_touchpad", "None"),i18nd("kcm_touchpad", "Touchpad edges")]
                        if(touchpad.scrollEdge){
                            scrollMethod.currentIndex=1
                        }else{
                            scrollMethod.currentIndex=0
                        }
                        
                    }
                }

                function syncCurrent() {
                    if (enabled && !root.loading) {
                        touchpad.scrollTwoFinger = (scrollMethod.currentIndex==1)?true:false
                        touchpad.scrollEdge =  (scrollMethod.currentIndex==2)?true:false
                        root.changeSignal()
                    }
                    loading = true
                    naturalScroll.load()
                    loading = false
                }
                onCurrentIndexChanged:scrollMethod.syncCurrent()
            }
            Rectangle{
                anchors.top: parent.top
                anchors.topMargin: 47
                width:parent.width 
                color:spaceline
                height:1
            }
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 61
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Invert scroll direction (Natural scrolling)")
                font.pixelSize: 14
                color: naturalScroll.enabled?textcolor:unabletextcolor
                font.family: "SourceHanSansSC-Regular"
            }

            KcmSwitch{
                id:naturalScroll
                anchors.top: parent.top
                anchors.topMargin: 61
                anchors.left: parent.left
                anchors.leftMargin: 498
                function load() {
                    enabled = touchpad.supportsNaturalScroll
                    checked = enabled && touchpad.naturalScroll
                }

                onCheckedChanged: {
                    if (enabled && !root.loading) {
                        touchpad.naturalScroll = checked
                        root.changeSignal()
                    }
                }

                QQC2.ToolTip {
                    text: i18nd("kcm_touchpad", "Touchscreen like scrolling.")
                    visible: parent.hovered
                    delay: 1000
                }
            }

            Rectangle{
                anchors.top: parent.top
                anchors.topMargin: 94
                width:parent.width 
                color:spaceline
                height:1
            }
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 109
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Disable horizontal scrolling")
                font.pixelSize: 14
                color: disableHorizontalScrolling.enabled?textcolor:unabletextcolor
                font.family: "SourceHanSansSC-Regular"
            }
            KcmSwitch{
                id: disableHorizontalScrolling
                anchors.top: parent.top
                anchors.topMargin: 109
                anchors.left: parent.left
                anchors.leftMargin: 498
                function load() {
                    // visible = touchpad.supportsHorizontalScrolling
                    enabled = touchpad.supportsHorizontalScrolling
                    checked = enabled && !touchpad.horizontalScrolling
                }

                onCheckedChanged: {
                    if (enabled && !root.loading) {
                        touchpad.horizontalScrolling = !checked
                        root.changeSignal()
                    }
                }

                QQC2.ToolTip {
                    text: i18nd("kcm_touchpad", "Disable horizontal scrolling")
                    visible: parent.hovered
                    delay: 1000
                }


            }
            Rectangle{
                anchors.top: parent.top
                anchors.topMargin: 141
                width:parent.width 
                color:spaceline
                height:1
                visible:touchpad.supportsScrollFactor
            }
            QQC2.Label{
                visible:touchpad.supportsScrollFactor
                anchors.top: parent.top
                anchors.topMargin: 155
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Scrolling speed")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            KcmSlider {
                id: scrollFactor
                visible:touchpad.supportsScrollFactor
                anchors.top: parent.top
                anchors.topMargin: 164
                anchors.left: parent.left
                anchors.leftMargin:342
                from: 0
                to: 14
                stepSize :1
                property variant values : [
                    0.1,
                    0.3,
                    0.5,
                    0.75,
                    1, // default
                    1.5,
                    2,
                    3,
                    4,
                    5,
                    7,
                    9,
                    12,
                    15,
                    20
                ]
                function load() {
                    let index = values.indexOf(touchpad.scrollFactor)
                    if (index === -1) {
                        index = values.indexOf(1);
                    }
                    value = index
                }

                onMoved: {
                    touchpad.scrollFactor = values[value]
                    root.changeSignal()
                }

                
            }
        }
    }

    //RIGHTCLICK
    Rectangle{
        id:rightclickitem
        anchors.top : scrollitem.bottom
        anchors.horizontalCenter:parent.horizontalCenter
        color: "transparent" 
        height:87
        width:558
        QQC2.Label{
            anchors.top: parent.top
            anchors.topMargin: 16
            anchors.left: parent.left
            text: i18nd("kcm_touchpad", "Right-click")
            font.pixelSize: 14
            color: textcolor
            font.family: "SourceHanSansSC-Regular"
        }
        Rectangle {
            anchors.top: parent.top
            anchors.topMargin: 39
            anchors.left: parent.left
            color: backgroundcolor
            height:48
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Right-click")
                font.pixelSize: 14
                color: rightClickMethod.enabled?textcolor:unabletextcolor
                font.family: "SourceHanSansSC-Regular"
            }
            QQC2.ComboBox {
                id: rightClickMethod
                anchors.top: parent.top
                anchors.topMargin: 9
                anchors.left: parent.left
                anchors.leftMargin: 344
                width: 192
                height:30
                model:[i18nd("kcm_touchpad", "Press bottom-right corner"), i18nd("kcm_touchpad", "Press anywhere with two fingers")]
                function load() {
                    enabled = touchpad.supportsClickMethodAreas && touchpad.supportsClickMethodClickfinger
                    if(!enabled){
                        return
                    }
                    if(touchpad.clickMethodAreas){
                        rightClickMethod.currentIndex=0
                    }else{
                        rightClickMethod.currentIndex=1
                    }
                }

                function syncCurrent() {
                    if (enabled && !root.loading) {
                        touchpad.clickMethodAreas = (rightClickMethod.currentIndex==0)?true:false
                        touchpad.clickMethodClickfinger = (rightClickMethod.currentIndex==1)?true:false
                        root.changeSignal()
                    }
                    loading = true
                    middleClickMethod.load()
                    root.updateview()
                    loading = false
                }
                onCurrentIndexChanged:rightClickMethod.syncCurrent()

            }
            
        }
    }
    //MIDDLECLICK
    Rectangle{
        id:midclickitem
        anchors.top : rightclickitem.bottom
        anchors.horizontalCenter:parent.horizontalCenter
        color: "transparent" 
        // visible:touchpad.middleEmulation && middleClickMethod.enabled
        visible:false
        height:visible? 134:0
        width:558
        QQC2.Label{
            anchors.top: parent.top
            anchors.topMargin: 16
            anchors.left: parent.left
            text:  i18nd("kcm_touchpad", "Middle-click: ")
            font.pixelSize: 14
            color: textcolor
            font.family: "SourceHanSansSC-Regular"
        }
        Rectangle {
            anchors.top: parent.top
            anchors.topMargin: 39
            anchors.left: parent.left
            color: backgroundcolor
            height:95
            width:558
            border.color: bordercolor
            border.width: 1
            radius: 10
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 23
                text: i18nd("kcm_touchpad", "Middle-click: ")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }    
            QQC2.ComboBox {
                id: middleClickMethod
                anchors.top: parent.top
                anchors.topMargin: 9
                anchors.left: parent.left
                anchors.leftMargin: 344
                width: 192
                height:30
                model:[i18nd("kcm_touchpad", "Press bottom-middle"),i18nd("kcm_touchpad", "Press bottom left and bottom right corners simultaneously")]
                function load() {
                    // midclickitem.visible=touchpad.middleEmulation && middleClickMethod.enabled
                    midclickitem.visible=false
                    midclickitem.height =visible? 134:0
                    enabled = touchpad.supportsMiddleEmulation
                    if(!enabled){
                        middleClickMethod.currentIndex=-1
                        return
                    }
                    if (touchpad.middleEmulation) {
                        middleClickMethod.currentIndex=1
                    } else {
                        middleClickMethod.currentIndex=0
                    }
                }

                function syncCurrent() {
                    if (enabled && !root.loading) {
                        touchpad.middleEmulation = (middleClickMethod.currentIndex==1)?true:false
                        root.changeSignal()
                    }
                    loading = true
                    middleEmulation.load()
                    loading = false
                }
                onCurrentIndexChanged:middleClickMethod.syncCurrent()
            }        
            
            Rectangle{
                anchors.top: parent.top
                anchors.topMargin: 47
                width:parent.width 
                color:spaceline
                height:1
            }
            QQC2.Label{
                anchors.top: parent.top
                anchors.topMargin: 61
                anchors.left: parent.left
                anchors.leftMargin: 23
                text:  i18nd("kcm_touchpad", "Press anywhere with three fingers")
                font.pixelSize: 14
                color: textcolor
                font.family: "SourceHanSansSC-Regular"
            }
            KcmSwitch{
                id: clickfingerMiddleInfoBox
                anchors.top: parent.top
                anchors.topMargin: 61
                anchors.left: parent.left
                anchors.leftMargin: 498
                checked: true
                enabled: false
            }            
            
        }
        
    }
}
}






       
 



    

    

        
